Flutter开发windows应用methodChannel调用原生能力

您所在的位置:网站首页 flutter 列表 Flutter开发windows应用methodChannel调用原生能力

Flutter开发windows应用methodChannel调用原生能力

#Flutter开发windows应用methodChannel调用原生能力| 来源: 网络整理| 查看: 265

Flutter 与原生native通讯主要有:methodChannel、eventChannel、basicMessageChannel。

这里使用methodchannel

注意:目录结构

lib/

    main.dart

windows/

    Runner/

      main.cpp

      flutter_window.cpp

      flutter_window.h

      CMakeLists.txt

1、在flutter_window.h文件中:添加如下头文件

#include

#include

#include

#include

#include

#include

2、在flutter_window.cpp文件中:添加头文件,

#include "flutter/generated_plugin_registrant.h"

并在bool FlutterWindow::OnCreate() {} 中添加如下代码:

RegisterPlugins(flutter_controller_->engine());

    flutter::MethodChannel channel(

        flutter_controller_->engine()->messenger(), "my_native_method_channel",

        &flutter::StandardMethodCodec::GetInstance());

    channel.SetMethodCallHandler(

          [](const flutter::MethodCall& call,

            std::unique_ptr result) {

            if (call.method_name() == "doSomething") {

              //TODO

              //在这里编写实现原生功能的代码

            } else {

              result->NotImplemented();

            }

          });

    SetChildContent(flutter_controller_->view()->GetNativeWindow());

    return true;

3、在main.dart文件中,

// 创建平台通道

  static const platform = MethodChannel('my_native_method_channel');

//调用

void _doSomething()async{

    await platform.invokeMethod("doSomething");

  }

以上即在flutter中通过methodChannel调用windows原生能力的目录结构及示例,具体功能可根据自己需求在TODO中完成c++代码编写,调用windows系统api.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3